﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using DCSLibrary;
using Telerik.Web.UI;
using Utils.DC.Web.UI;
using Utils.DCSControl;
using Utils.Entity;

public partial class Admin_Module_thay_doi_thong_tin_nguoi_dung : System.Web.UI.UserControl
{
    #region Cac bien dung chung
    UsersUI objUser = new UsersUI();
    MessageBox messageBox = new MessageBox();
    #endregion

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            UserEntity users = ((dcPage)this.Page).CurrentUser;

            if (users != null)
            {
                LoadEditData(users.UserID);
            }
        }
    }

    protected void btnCreate_Click(object sender, EventArgs e)
    {
        if (txtUserID.Value.Trim() != "")
        {
            try
            {
                int result = objUser.Update(Convert.ToInt32(txtUserID.Value), txtUserName.Text.Trim(), txtEmail.Text.Trim(), txtFirstName.Text.Trim(), txtLastName.Text.Trim(), (DateTime)txtBirthday.SelectedDate,
                    Convert.ToByte(cboSex.SelectedValue), txtMobile.Text.Trim(), txtAddress.Text.Trim(), txtCompanyName.Text, txtCompanyAddress.Text, txtFax.Text, txtPosition.Text, txtJob.Text);
                if (result > 0)
                {
                    Response.Write(messageBox.Alert(MessageBox.MESSAGE_UPDATE_SUCCESSFULLY,"/"));
                }
                else
                {
                    Response.Write(messageBox.Alert(MessageBox.MESSAGE_UPDATE_NOT_SUCCESSFULLY,"/"));
                }
            }
            catch (Exception ex)
            {
                Response.Write(messageBox.Alert(MessageBox.MESSAGE_CONFILCT_DATA));
            }
        }
    }

    protected void btnReset_Click(object sender, EventArgs e)
    {
        ReInsertData();
    }

    private void LoadEditData(int ID)
    {
        DataSet ds = objUser.GetByID(ID);
        if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
        {
            DataRow row = ds.Tables[0].Rows[0];
            txtUserID.Value = row["UserID"].ToString();
            txtUserName.Text = row["UserName"].ToString().Trim();
            txtEmail.Text = row["Email"].ToString().Trim();
            txtPosition.Text = row["Position"].ToString().Trim();
            txtAddress.Text = row["Position"].ToString().Trim();
            txtBirthday.SelectedDate = (DateTime?)Convert.ToDateTime(row["Birthday"]);
            txtCompanyAddress.Text = row["CompanyAddress"].ToString().Trim();
            txtCompanyName.Text = row["CompanyName"].ToString().Trim();
            txtFax.Text = row["Fax"].ToString().Trim();
            txtFirstName.Text = row["FirstName"].ToString().Trim();
            txtJob.Text = row["Job"].ToString().Trim();
            txtLastName.Text = row["LastName"].ToString().Trim();
            txtMobile.Text = row["Mobile"].ToString().Trim();
            cboSex.SelectedValue = row["Sex"].ToString().Trim();
        }
    }

    private void ReInsertData()
    {
        txtUserName.Text = "";
        txtEmail.Text = "";
        txtPosition.Text = "";
        txtAddress.Text = "";
        txtBirthday.SelectedDate = (DateTime?)DateTime.Now;
        txtCompanyAddress.Text = "";
        txtCompanyName.Text = "";
        txtFax.Text = "";
        txtFirstName.Text = "";
        txtJob.Text = "";
        txtLastName.Text = "";
        txtMobile.Text = "";
        cboSex.SelectedValue = "";
    }
}
